<?php
/**
 * Created by PhpStorm.
 * User: Gooraye
 * Date: 2016/7/12
 * Time: 16:44
 */
namespace Mengwu\Api;

use Bbs\Model\VUserInfoModel;
use Common\Api\Api;
use Think\Model;

class YuerjingReplyViewApi extends Api{
    const QUERY = "Mengwu/YuerjingReplyView/query";

    /**
     * 用户收到育儿经评论分页
     */
    const QUERY_WITH_UID = "Mengwu/YuerjingReplyView/queryWithUid";

    /**
     * 用户育儿经评论分页
     */
    const QUERY_USER_COMMENT = "Mengwu/YuerjingReplyView/queryUserComment";

    protected function _init(){

    }

    public function query($tid,$page = ['curpage'=>0,'size'=>10]){
        $model = new Model();
        $result = $model->query("select * from (select `author_id`,`dateline`,`message` as `comment`,0 as `to_uid`,`pid` from `itboye_forum_post` where `tid`=%d union select `author_id`,`dateline`,`comment`,`to_uid`,`pid` from `itboye_forum_postcomment` where `tid`=%d) as view order by view.dateline desc limit %d,%d",$tid,$tid,$page['curpage']*$page['size'],$page['size']);

        if($result===false){
            $error = $model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count =  $model->query("select count(*) as `count` from (select `author_id`,`dateline`,`message` as `comment`,0 as `to_uid`,`pid` from `itboye_forum_post` where `tid`=%d union select `author_id`,`dateline`,`comment`,`to_uid`,`pid` from `itboye_forum_postcomment` where `tid`=%d) as view",$tid,$tid);
        $count = $count[0]['count'];

        //查找用户昵称
        $list = $result;
        foreach($list as &$val){
            $result = $this->getUserInfo($val['author_id']);
            if($result['status']) $val['author_info'] = $result['info'];
            if($val['to_uid']!=0){
                $result = $this->getUserInfo($val['to_uid']);
                if($result['status']) $val['reply_info'] = $result['info'];
            }
        }

        return $this->apiReturnSuc(['count'=>$count, 'list'=>$list]);

    }

    private function getUserInfo($uid){
        $model = new VUserInfoModel();

        $map = ['id' => $uid];

        $field = "id,username,nickname,head";
        $result = $model->where($map)->field($field)->find();

        if($result === false){
            $error = $model->getDbError();
            return $this->apiReturnErr($error);
        }
        return $this->apiReturnSuc($result);
    }

    public function queryWithUid($uid, $page = ['curpage'=>0,'size'=>10])
    {
        $model = new Model();
        $limit = $page['curpage'] * $page['size'] . ',' . $page['size'];

        $query = "SELECT * FROM 
                  (SELECT  c.`dateline`,c.`comment`,`to_uid`,c.`tid`,c.`author_id`,p.`message` as `re_comment`,p.`fid` FROM `itboye_forum_postcomment` AS c
                              LEFT JOIN `itboye_forum_post` AS p ON c.pid = p.pid WHERE `to_uid` = $uid AND c.`author_id` != $uid AND p.`fid` IN (7,8)
                  UNION SELECT p.dateline,p.message AS `comment`,0 AS `to_uid`,p.tid,p.author_id,t.`subject` as `re_comment`,p.`fid` FROM `itboye_forum_thread` AS `t` 
                              left join `itboye_forum_post` as p on t.tid = p.tid where t.author_id = $uid and p.author_id != $uid AND p.`fid` IN (7,8)) 
                  as view_comment order by dateline desc limit $limit";
        $list = $model->query($query);


        if ($list === false) {
            $error = $model->getDbError();
            return $this->apiReturnErr($error);
        }

        $query = "SELECT count(*) AS `count` FROM 
                  (SELECT  c.`dateline`,c.`comment`,`to_uid`,c.`tid`,c.`author_id`,p.`message` as `re_comment` FROM `itboye_forum_postcomment` AS c
                              LEFT JOIN `itboye_forum_post` AS p ON c.pid = p.pid WHERE `to_uid` = $uid AND c.`author_id` != $uid AND p.`fid` IN (7,8)
                  UNION SELECT p.dateline,p.message AS `comment`,0 AS `to_uid`,p.tid,p.author_id,t.`subject` as `re_comment` FROM `itboye_forum_thread` AS `t` 
                              left join `itboye_forum_post` as p on t.tid = p.tid where t.author_id = $uid and p.author_id != $uid AND p.`fid` IN (7,8)) 
                  as view_comment";


        $count = $model->query($query);
        $count = $count[0]['count'];

        //查找用户昵称
        foreach ($list as &$val) {
            $result = $this->getUserInfo($val['author_id']);
            if ($result['status']) $val['author_info'] = $result['info'];
            if ($val['to_uid'] != 0) {
                $result = $this->getUserInfo($val['to_uid']);
                if ($result['status']) $val['reply_info'] = $result['info'];
            }
        }

        return $this->apiReturnSuc(['count' => $count, 'list' => $list]);
    }

    public function queryUserComment($uid, $page = ['curpage'=>0,'size'=>10]){
        
        $model = new Model();
        $limit = $page['curpage'] * $page['size'] . ',' . $page['size'];

        $query = "SELECT * FROM
                  (SELECT c.`dateline`,c.`comment`,`to_uid`,c.`tid`,c.`author_id`,p.`message` as `re_comment`,p.`fid` FROM `itboye_forum_postcomment` as c
                            LEFT JOIN `itboye_forum_post` as p ON c.pid = p.pid WHERE c.author_id = $uid AND p.`fid` IN (7,8)
                  UNION SELECT p.dateline,p.message AS `comment`,0 AS `to_uid`,p.tid,p.author_id,t.`subject` AS `re_comment`,p.`fid` FROM `itboye_forum_thread` AS `t`
                            LEFT JOIN `itboye_forum_post` AS p ON t.tid = p.tid where p.author_id = $uid AND p.`fid` IN (7,8))
                  AS view_comment ORDER BY dateline DESC LIMIT $limit";
        $list = $model->query($query);

        $query = "SELECT count(*) AS `count` FROM
                  (SELECT c.`dateline`,c.`comment`,`to_uid`,c.`tid`,c.`author_id`,p.`message` as `re_comment`,p.`fid` FROM `itboye_forum_postcomment` as c
                            LEFT JOIN `itboye_forum_post` as p ON c.pid = p.pid WHERE c.author_id = $uid AND p.`fid` IN (7,8)
                  UNION SELECT p.dateline,p.message AS `comment`,0 AS `to_uid`,p.tid,p.author_id,t.`subject` AS `re_comment`,p.`fid` FROM `itboye_forum_thread` AS `t`
                            LEFT JOIN `itboye_forum_post` AS p ON t.tid = p.tid where p.author_id = $uid AND p.`fid` IN (7,8))
                  AS view_comment";

        $count = $model->query($query);
        $count = $count[0]['count'];

        //查找用户昵称
        foreach ($list as &$val) {
            $result = $this->getUserInfo($val['author_id']);
            if ($result['status']) $val['author_info'] = $result['info'];
            if ($val['to_uid'] != 0) {
                $result = $this->getUserInfo($val['to_uid']);
                if ($result['status']) $val['reply_info'] = $result['info'];
            }
        }

        return $this->apiReturnSuc(['count' => $count, 'list' => $list]);

        
    }

}